﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Model;
using DataAccess;

namespace Project
{
    /// <summary>
    /// Lógica de interacción para AllPsicologistReports.xaml
    /// </summary>
    public partial class AllPsicologistReports : UserControl
    {
        public List<PsicologistReportClass> allReports;
        public List<PsicologistReportInfo> reportsToShow;
        public long id;

        public AllPsicologistReports()
        {
            InitializeComponent();
        }

        public AllPsicologistReports(long comp)
        {
            InitializeComponent();
            UIWindowManager.SetWindowTitle(this, "Brigada De Protección a la Familia - AllPsicologistReports");
            //tbCI.IsEnabled = false;
            reportsToShow = new List<PsicologistReportInfo>();
            allReports = PsicologistReportDBAccess.GetAllPsicologistReport(comp);
            fillInfoList(allReports);
            listView.DataContext = reportsToShow;
            lbCase.Content = comp;
            //tbCI.AddValitationRule(new BlankValidRule());
            //tbName.AddValitationRule(new BlankValidRule());
            if (UserControllerAndDataAccess.IsUserLogged())
            {
                if (!UserControllerAndDataAccess.IsPsicologistLogged)
                {
                    Modify.Visibility = Visibility.Collapsed;
                }
            }
        }

        public void fillInfoList(List<PsicologistReportClass> list)
        {
            PsicologistReportInfo info = new PsicologistReportInfo();
            foreach (PsicologistReportClass e in list)
            {
                info = fillInfo(e);
                reportsToShow.Add(info);
            }
            listView.DataContext = reportsToShow;
        }

        public PsicologistReportInfo fillInfo(PsicologistReportClass e)
        {
            PsicologistReportInfo f = new PsicologistReportInfo();
            Model.EntityModel.Person person = PersonAndActorDataAccess.GetPersonById(e.IdPerson.ToString());
            f.CiPatient = person.CiPassport;
            f.NamePatient = person.Name + " " + person.PaternalName;
            Functionary employee = DataBaseAccess.ReadEmployee(e.CiPsicologist);
            f.CiPsicologist = employee.CI;
            f.NamePsicologist = employee.Name + " " + employee.PaternalName;
            f.Date = e.Date.ToShortDateString() + " " + e.Time;
            f.Id = e.Id;
            if (String.IsNullOrEmpty(employee.MaternalName))
                f.NamePsicologist = f.NamePsicologist + " ";
            else
                f.NamePsicologist = f.NamePsicologist + " " + employee.MaternalName;
            if (String.IsNullOrEmpty(person.MaternalName))
                f.NamePatient = f.NamePatient + " ";
            else
                f.NamePatient = f.NamePatient + " " + employee.MaternalName;
            return f;
        }
        /*
        private void FilterResults(List<PsicologistReportClass> psicologist)
        {
            PsicologistReportInfo info = new PsicologistReportInfo();
            reportsToShow.Clear();
            listView.DataContext = null;
                        
            if (!String.IsNullOrEmpty(tbCI.Text) && rbtnByCi.IsChecked == true)
            {
                foreach (PsicologistReportClass e in psicologist)
                {
                    info = fillInfo(e);
                    if (containsString(e.CiPsicologist.ToString(), tbCI.Text) || containsString(info.CiPatient.ToString(),tbCI.Text))
                    {
                        reportsToShow.Add(info);
                    }
                }
            }
            else
            {
                if (!String.IsNullOrEmpty(tbName.Text) && rbtnByName.IsChecked == true)
                {
                    foreach (PsicologistReportClass e in psicologist)
                    {
                        info = fillInfo(e);
                        if (containsString(info.NamePatient, tbName.Text) || containsString(info.NamePSicologist, tbName.Text))
                        {
                            reportsToShow.Add(info);
                        }
                    }
                }
                else
                    fillInfoList(psicologist);
            }
            listView.DataContext = reportsToShow;
        }
        */
        public bool containsString(string cad1, string cad2)
        {
            if (cad1 != null)
            {
                for (int i = 0; i < cad2.Length; i++)
                {
                    if (cad1[i] != cad2[i])
                        return false;
                }
                return true;
            }
            else
                return false;
        }

        private void View_Click(object sender, RoutedEventArgs e)
        {
            if (listView.SelectedItem as PsicologistReportInfo != null)
            {
                //id = (listView.SelectedItem as PsicologistReportInfo).Id;
                Content.Children.Clear();
                Content.Children.Add(new ViewPsicologistReport(allReports[listView.SelectedIndex]));
            }
        }

        private void Edit_Click(object sender, RoutedEventArgs e)
        {
            if (listView.SelectedItem as PsicologistReportInfo != null)
            {
                //id = (listView.SelectedItem as PsicologistReportInfo).Id;
                Content.Children.Clear();
                Content.Children.Add(new RegisterPsicologistReport(allReports[listView.SelectedIndex]));
            }
        }
        /*
        private void rbtnByName_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            tbName.IsEnabled = true;
            tbCI.IsEnabled = false;
        }

        private void rbtnByCi_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            tbName.IsEnabled = false;
            tbCI.IsEnabled = true;
        }*/
    }
}

